﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ThemeCupid.FrontEnd.Logic;
using ThemeCupid.FrontEnd.Model;

namespace ThemeCupid.FrontEnd
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        ProductContext db=new ProductContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ShoppingCartAction usersShoppingCart = new ShoppingCartAction();
                decimal cartTotal = 0;
                cartTotal = usersShoppingCart.GetTotal();
                if (cartTotal > 0)
                {
                    // Display Total.
                    lblTotal.Text = String.Format("{0:c}", cartTotal);
                }
                else
                {
                    lblTotal.Text = "";
                    //ShoppingCartTitle.InnerText = "Shopping Cart is Empty";
                    //UpdateBtn.Visible = false;
                    ////CheckoutImageBtn.Visible = false;
                }
                var items = GetShoppingCartItems();

                CartList.DataSource = items;

                CartList.DataBind();
                totalPrice.InnerText = items.Sum(x => x.TotalPrice).ToString("0:c");
            }
          
            
        }
        public List<CartItem> GetShoppingCartItems()
        {
            ShoppingCartAction actions = new ShoppingCartAction();
            return actions.GetCartItems();
        }
       
        //public static IOrderedDictionary GetValues(DataListItem row)
        //{
        //    IOrderedDictionary values = new OrderedDictionary();
        //    foreach (DataControlFieldCell cell in row.FindControl("serializeId").)
        //    {
        //        if (cell.Visible)
        //        {
        //            // Extract values from the cell.
        //            cell.ContainingField.ExtractValuesFromCell(values, cell, row.RowState, true);
        //        }
        //    }
        //    return values;
        //}

        protected void UpdateBtn_Click(object sender, EventArgs e)
        {
            //HtmlButton button = (HtmlButton)sender;
            //var productId = button.Attributes["data"];
            
      
            //ShoppingCartAction usersShoppingCart = new ShoppingCartAction();
            //String cartId = usersShoppingCart.GetCartId();
            //TextBox quantityTextBox = new TextBox();

            //qualityId.Text = Convert.ToInt16(quantityTextBox.Text.ToString());
            ////}
            //usersShoppingCart.UpdateShoppingCartDatabase(cartId, cartUpdates);
            //CartList.DataBind();
            //lblTotal.Text = String.Format("{0:c}", usersShoppingCart.GetTotal());
         
        }
        protected void CheckoutBtn_Click(object sender, ImageClickEventArgs e)
        {
            ShoppingCartAction usersShoppingCart = new ShoppingCartAction();
            Session["payment_amt"] = usersShoppingCart.GetTotal();
            Response.Redirect("Checkout/CheckoutStart.aspx");
        }
        protected void datalist_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {

                HtmlInputText label5 = (HtmlInputText)e.Item.FindControl("qualityId");
                
                // now you have access for each bound row
            }
        }
        protected void item_commandUpdate(object sender, DataListCommandEventArgs e)
        {
            if (e.CommandName == "updateCart")
            {
                     string productId = e.CommandArgument.ToString();
                    HtmlInputText label5 = (HtmlInputText) e.Item.FindControl("qualityId");

                    ShoppingCartAction usersShoppingCart = new ShoppingCartAction();
                    ShoppingCartAction.ShoppingCartUpdates aa = new ShoppingCartAction.ShoppingCartUpdates();
                    aa.ProductId = productId;
                    aa.PurchaseQuantity = Convert.ToInt32(label5.Value);

                    usersShoppingCart.UpdateShoppingCartDatabase(usersShoppingCart.GetCartId(),
                                                                 new ShoppingCartAction.ShoppingCartUpdates[] {aa});
            }
            
        }
    }
}